//
//  CRheadVideoController.m
//  NewAge
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRheadVideoController.h"
#import "CRVideoModel.h"
#import "CRMyCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UIImageView+LK.h"

@interface CRheadVideoController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *arr;

@property(nonatomic, strong)MPMoviePlayerViewController *movieView;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, assign) CGRect selectedRect;

@end

@implementation CRheadVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self createData];
}

#pragma mark - createData
- (void)createData {
    [MBProgressHUDManager showHUDtoView:self.view string:@"加载中"];
    
    [AfNetWorkingManager getInfoWithUrl:[NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/%@/y/0-10.html", self.sid] block:^(NSDictionary *resultDic, NSError *error) {
        
        self.arr = [CRVideoModel baseModelByArr:resultDic[self.sid]];
        NSLog(@"11111111 ===  %@", self.arr);
        
        [self.tableView reloadData];
        [MBProgressHUDManager removeHUD];
    }];
}

#pragma mark - createView
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.rowHeight = HEIGHT / 2;
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

#pragma mark - Delegate & DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CRVideoModel *model = self.arr[indexPath.row];
    static NSString *reuse = @"reuse";
    CRMyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[CRMyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.myLabel.text = model.title;
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    
    NSString *ss = [NSString stringWithFormat:@"%@", model.playCount];
    cell.twoLabel.text = model.descriptionVC;
    NSString *str = [model.ptime substringWithRange:NSMakeRange(11, 5)];
    cell.threeLabel.text = [NSString stringWithFormat:@"时间:%@      阅读量:%.1f万", str, [ss floatValue]/10000];
    
    // 点击cell时的颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 点击手势
    cell.myImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [cell.myImageView addGestureRecognizer:tap];
    
    // 点几下才能触发的方法
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;

    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)tapAction:(UITapGestureRecognizer *)sender{
    if (self.movieView.view) {
        // 移除
        [self.movieView.view removeFromSuperview];
    }
    UIView *view = sender.view;
    UITableViewCell *cell = (UITableViewCell *)view.superview.superview;
    self.indexPath = [self.tableView indexPathForCell:cell];
    CRVideoModel *model = self.arr[self.indexPath.row];
    self.movieView = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:model.mp4_url]];
    self.movieView.view.frame = CGRectMake(10, 75, [UIScreen mainScreen].bounds.size.width - 20, HEIGHT / 3);
    [self.movieView.moviePlayer setScalingMode:(MPMovieScalingModeAspectFit)];
    [self.movieView.moviePlayer setControlStyle:(MPMovieControlStyleEmbedded)];
    [cell addSubview:self.movieView.view];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CRMyCell *cell = (CRMyCell *)[self.tableView cellForRowAtIndexPath:self.indexPath];
    CGRect rectInTable = [self.tableView rectForRowAtIndexPath:self.indexPath];
    CGRect rectInWindow =[self.tableView convertRect:rectInTable toView:[self.tableView superview]];
    self.selectedRect = CGRectMake(rectInTable.origin.x + 10, rectInTable.origin.y + 75, cell.myImageView.bounds.size.width, cell.myImageView.bounds.size.height);
    if ([self.movieView.moviePlayer isPreparedToPlay]) {
        
        if (rectInWindow.origin.y + HEIGHT / 2 < 0 || rectInWindow.origin.y > [UIScreen mainScreen].bounds.size.height-cell.myImageView.bounds.size.height) {
            [UIView animateWithDuration:0.5 animations:^{
                // 小窗口尺寸
                self.movieView.view.frame = CGRectMake(self.view.bounds.size.width-180, self.view.bounds.size.height-150, 170, 150);
                [self.view addSubview:self.movieView.view];
                self.movieView.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
            }];
            
        }else{
            self.movieView.view.frame = self.selectedRect;
            [self.tableView addSubview:self.movieView.view];
            self.movieView.moviePlayer.controlStyle =MPMovieControlStyleDefault;
        }
        
    }

    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
